package vip.fastgo.retroid.android.gamebox.view.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.Button
import androidx.constraintlayout.widget.ConstraintLayout

/**
 * ABXY，按钮操作View
 */
class ABXYView(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : ConstraintLayout(context, attrs, defStyleAttr) {

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context): this(context, null)

    private val aBtn = Button(context)
    private val bBtn = Button(context)
    private val xBtn = Button(context)
    private val yBtn = Button(context)
    var doClickListener: DoClickListener? = null

    init {
        addLeftBtn()
        addUpBtn()
        addRightBtn()
        addDownBtn()
    }

    private fun addLeftBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        aBtn.layoutParams = layoutParams
        aBtn.text = "A"
        setClickListener(aBtn, ABXY.A)
        addView(aBtn)
    }

    private fun addUpBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        xBtn.layoutParams = layoutParams
        xBtn.text = "X"
        setClickListener(xBtn, ABXY.X)
        addView(xBtn)
    }

    private fun addRightBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.topToTop = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        yBtn.layoutParams = layoutParams
        yBtn.text = "Y"
        setClickListener(yBtn, ABXY.Y)
        addView(yBtn)
    }

    private fun addDownBtn() {
        val layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        layoutParams.leftToLeft = LayoutParams.PARENT_ID
        layoutParams.rightToRight = LayoutParams.PARENT_ID
        layoutParams.bottomToBottom = LayoutParams.PARENT_ID
        bBtn.layoutParams = layoutParams
        bBtn.text = "B"
        setClickListener(bBtn, ABXY.B)
        addView(bBtn)
    }

    private fun setClickListener (view: View, abxy: ABXY) {
        view.setOnClickListener {
            doClickListener?.doClick(view, abxy)
        }
    }

    interface DoClickListener {
        fun doClick(view: View, abxy: ABXY)
    }

    enum class ABXY {
        A,B,X,Y
    }
}